package cn.edu.seu.herald.informationplatform.dao.database;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import cn.edu.seu.herald.informationplatform.util.logger.Logger;

public abstract class DataAccessObject {
	
	private static Logger _logger;
	
	static {
		_logger = Logger.getInstance(DataAccessObject.class);
	}
	
	protected Connection getConnectionLeague() {
		try {
			Context initCtx = new InitialContext();
			Context envCtx = (Context) initCtx.lookup("java:comp/env");

			DataSource ds = (DataSource)
			envCtx.lookup("jdbc/league");

			Connection conn = ds.getConnection();
			return conn;
		} catch(Exception e) {
			e.printStackTrace();
			//_logger.error("exception " + e.toString() +
			//		" occurred during getConnectionLeague()");
			return null;
		}
	}
	
	protected void closeCon(Connection con) {
		try {
			if (con!=null) {
				con.close();
			}
		} catch (SQLException e) {
			_logger.error("exception " + e.toString() + " occurred during closeCon()");
		}
	}
	
	protected static void closeRs(ResultSet rs) {
		try {
		    rs.close();
		} catch (SQLException e) {
			_logger.error("exception " + e.toString() + " occurred during closeRs()");
		}
	}

}
